﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.Framework;
using System.Windows.Media.Animation;

namespace myBoard.App.Controls
{
	public partial class MorphingItem
	{
        private MagnetEx _magnet;
        public Point FirstClickPoint { get; set; }
        private Storyboard _morphToThumbnailSB;
        private Storyboard _morphToIconSB;
        private bool _isMorphingToThumbnail;
        private bool _isMorphingToIcon;


        public MagnetEx Magnet 
        {
            get
            {
                return _magnet;
            }
            set
            {
                _magnet = value;
                UpdateMorphToThumbnailStoryboard();
            }
        }

		public MorphingItem()
		{
			this.InitializeComponent();
            _morphToIconSB = this.Resources["MorphToIcon"] as Storyboard;
            _morphToThumbnailSB = this.Resources["MorphToThumbnail"] as Storyboard;
		}

        public void MorphToIcon()
        {
            if (!_isMorphingToIcon)
            {
                _isMorphingToIcon = true;
                _isMorphingToThumbnail = false;
                this.BeginStoryboard(_morphToIconSB);
            }
        }

        public void MorphToThumbnail()
        {
            if (!_isMorphingToThumbnail)
            {
                _isMorphingToThumbnail = true;
                _isMorphingToIcon = false;
                this.BeginStoryboard(_morphToThumbnailSB);
            }
            
        }

        private void UpdateMorphToThumbnailStoryboard()
        {
            double thumbnailXScaleFactor =  Magnet.Magnet.Thumbnail.PixelWidth / _thumbnailImage.Width;
            double thumbnailYScaleFactor = Magnet.Magnet.Thumbnail.PixelHeight / _thumbnailImage.Height;
            double thumbnailScaleFactor = Math.Max(thumbnailXScaleFactor, thumbnailYScaleFactor);
            

            foreach (Timeline timeline in _morphToThumbnailSB.Children)
            {
                if (timeline.Name == "ThumbnailXScaleTimeline" ||
                    timeline.Name == "ThumbnailYScaleTimeline")
                {
                    ((DoubleAnimationUsingKeyFrames)timeline).KeyFrames[1].Value = thumbnailScaleFactor;
                }

                if(timeline.Name == "IconXScaleTimeline")
                {
                    ((DoubleAnimationUsingKeyFrames)timeline).KeyFrames[1].Value = thumbnailXScaleFactor;
                }
                if (timeline.Name == "IconYScaleTimeline")
                {
                    ((DoubleAnimationUsingKeyFrames)timeline).KeyFrames[1].Value = thumbnailYScaleFactor;
                }
            }
        }
	}
}